package com.bee.health;

import java.util.Random;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HeartrateActivity extends Activity implements OnClickListener{
	private ImageView iv;
	private Button bt_test;
	private TextView tv_heartcount;
	private MediaPlayer mediaPlayer;
	private TextView tv_cladvice;
	private boolean isExcute=true;
	Handler myHandler = new Handler(){
		 public void handleMessage(Message msg) {   
             switch (msg.what) {   
	             case 0:
	            	 	int count = showCount();
	            	   tv_heartcount.setText("您的心跳每分钟是："+count);
	            	   setAdvice(count);
						bt_test.setText("开始测量");
						bt_test.setClickable(true);//设置为可以点击
						stopHeart();
				  break;
             }   
             super.handleMessage(msg);   
        }

	
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_heartrate);
		init();
	}
	/**
	 * 按钮点击事件
	 * @param v
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
	      case R.id.bt_test:
	    	  
	    
	    	  tv_heartcount.setText("您的心跳每分钟是:");//初始化
	    	  heartSound();//播放心跳的音乐
	    	  bt_test.setText("正在测量...");
	    	  bt_test.setClickable(false);//设置按钮不能再点击
	    	  Toast.makeText(this, "请保持平静，静待半分钟", Toast.LENGTH_LONG).show();
	    	  scale(v);//执行动画
	    	  waitChangeUI();
	        break;
	      default:
	        break;
	      }
	}
	/*
	 * 开始心跳声
	 */
	private void heartSound()
	{
			try {
			AssetManager am = getAssets();//获得该应用的AssetManager  
			AssetFileDescriptor afd = am.openFd("heartsound.mp3");  
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setDataSource(afd.getFileDescriptor());
			mediaPlayer.prepare(); //准备  
			mediaPlayer.start();
			} 
			catch (Exception e) {
				e.printStackTrace();
			}  
	}
	/**
	 * 停止心跳声音
	 * @param view
	 */
	private void stopHeart() {
		if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer=null;
		}
	}
	
	private void setAdvice(int count) {
		if(count>=70&&count<85)
		{
			 tv_cladvice.setText("您的心跳稍微为慢，说明您运动量不错，继续保持锻炼！");
		}else if(count>=85&&count<90){
			 tv_cladvice.setText("您的心跳正常，继续保持锻炼！");
		}else if(count>=90&&count<=100)
		{
			 tv_cladvice.setText("您的心跳有些快，一定要多加锻炼哦！");
		}
		
	}   
	/**
	 * 等待Ui改变
	 */
	private void waitChangeUI()
	{
		new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							Thread.sleep(18619);//休息20s
							
							 Message message = new Message();//更改ui
							 message.what = 0;
							 myHandler.sendMessage(message);
							 
						} catch (InterruptedException e) {
							e.printStackTrace();
						}	
					}
				}).start();
	}
	/**
	 * 显示心跳个数
	 * @return
	 */
	int s =0;//返回的心跳的个数
	private int showCount()
	{
		Random random = new Random();
		 
		if (isExcute)
		{
			 int max = 110;
		     int min = 70;
		     s = random.nextInt(max)%(max-min+1) + min;
		     //
		}
		else
		{
			 int max = s + 2;
		     int min = s - 2;
		     s = random.nextInt(max)%(max-min+1) + min;
		}
		isExcute = false;
	     return s;
	}

	/**
	 * 初始化
	 */
	private void init() 
	{
		bt_test = (Button) findViewById(R.id.bt_test);
		iv = (ImageView) findViewById(R.id.iv_heart);
		tv_heartcount = (TextView) findViewById(R.id.tv_heartcount);
		tv_cladvice = (TextView) findViewById(R.id.tv_cladvice);
		bt_test.setOnClickListener(this);
	}
	
	//缩放动画
	public void scale(View view){
		ScaleAnimation sa = new ScaleAnimation(0.5f, 1f, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		sa.setDuration(2000);
		sa.setRepeatCount(8);
		sa.setRepeatMode(Animation.REVERSE);
		iv.startAnimation(sa);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stopHeart();
	}
	
	
	

}
